package ihuotui.sample.webmagic.util;

import java.util.List;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;    

public class DownloadThread {

    Logger logger = LoggerFactory.getLogger(DownloadThread.class);

    public JedisPool jedisPool;
    public String queue;

    public DownloadThread(JedisPool jedisPool, String queue) {
        this.jedisPool = jedisPool;
        this.queue = queue;
    }

    public void run() {
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            public void run() {
                while (true) {
                    Jedis jedis = jedisPool.getResource();
                    try {
                        List<String> message = jedis.blpop(0, queue);
                        if (message == null || message.isEmpty() || message.size() < 2)
                            continue;
                        onMessage(message.get(1));
                    } finally {
                        jedis.close();
                    }
                }
            }

            private void onMessage(String string) {
                UrlFileBean urlFile = JSON.parseObject(string, UrlFileBean.class);
                String photoUrl = urlFile.getUrl();
                String Referer = urlFile.getReferer();
                String path = urlFile.getPath();
                try {
                    if (HttpClient.getInstantce().isFileExit(path, photoUrl)) {
                        logger.info("downloaded file " + photoUrl);
                        return;
                    }
                    logger.info("downloading file " + photoUrl);
                    HttpClient.getInstantce().save(path, photoUrl, Referer);
                } catch (Exception e) {
                    logger.error("var " + photoUrl, e);
                }
            }
        });
    }
}
